import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> list=new ArrayList<>();
        HashMap<String ,Integer> hash1=new LinkedHashMap<>();
        for (int i = 0; i <words.length ; i++) {
            String in=words[i];
            hash1.put(in,hash1.getOrDefault(in,0)+1);

        }
        int m=words[0].length();
        int len=words.length;
        for (int i = 0; i < m; i++) {
            HashMap<String ,Integer> hash2=new LinkedHashMap<>();
            int count=0;
            for (int right = i,left=i ; right +m<=s.length() ; right+=m) {
                String in=s.substring(right,right+m);
                hash2.put(in,hash2.getOrDefault(in,0)+1);
                if(hash2.get(in)<=hash1.getOrDefault(in,0)){
                    count++;
                }
                if(right-left>=m*len){
                    String out=s.substring(left,left+m);
                    if(hash2.get(out)<=hash1.getOrDefault(out,0)){
                        count--;
                    }
                    hash2.put(out,hash2.get(out)-1);
                    left+=m;

                }
                if(count==len){
                    list.add(left);
                }

            }
        }
        return list;


    }
}